﻿using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Xml;
namespace WPFExcelReport
{
    public partial class JLTAB55DOCReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public string doctype = "ABDOC";
        string operation = "OpenExcel";
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }


        public DataSet setUI = new DataSet();
        string DocType;

        string Today
        {
            get
            {
                DateTime now = DateTime.Now;
                return now.Year.ToString() + Right("00" + now.Month.ToString(), 2) + Right("00" + now.Day.ToString(), 2);
            }
        }
        #endregion
        public JLTAB55DOCReport()
            : base()
        {

        }
        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = false;
            try
            {
                if (exAgent == null)
                {
                    exAgent = new ExcelX.ExcelLib.ExcelAgent();
                }
                operation = fileType;
                Root = "C:\\IntexReport";
                strCompany = "JLT";
                strExcelFile = @"JLT\JLTAB55DOC.xls";
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\JLT\JLTAB55DOC.xls");
                DataRow row = dsSource.Tables["ABPOS"].Rows[0];
                string KRS = row["ABPAUFKRS"].ToString().Trim();
                string LFD = row["ABPAUFLFD"].ToString().Trim();
                string POS = row["ABPAUFPOS"].ToString().Trim();

                string newfilename = KRS + "-" + LFD + "-" + POS;
                switch (doctype)
                {
                    case "ABDOC":
                        Standard = "AB55DOC";
                        destfolder = SetFolder(Root, "织造单");
                        ServerShareFile(newfilename, "织造单", row);
                        break;
                    case "WEFTYARN":
                        Standard = "AB55WF";
                        destfolder = SetFolder(Root, "纬纱领用");
                        ServerShareFile(newfilename, "纬纱领用", row);
                        break;
                    case "WBBST":
                        Standard = "AB55WB";
                        destfolder = SetFolder(Root, "布卷标签");
                        ServerShareFile(newfilename, "布卷标签", row);
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                exAgent.Close();
                exAgent = null;
            }
        } 
        private string CreateFile(string orderno, DataRow row)
        {
            string filename = ""; 
            string pwd = "";
            exAgent.Open(TemplatePath);
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            string sheetName = orderno;
            string newfilename = orderno + ".xls";
            exAgent.CreateNewSheet(newfilename, Standard, sheetName, pwd);
            filename = exAgent.FileName;
            switch (doctype)
            {
                case "ABDOC":
                    FillInAB55PR();
                    break;
                case "WEFTYARN":
                    FillInAB55WF();
                    break;
                case "WBBST":
                    FillInAB55WB();
                    break;
            } 
            string localFile = exAgent.FileName;
            exAgent.Save();
            exAgent.Close();
            //System.Diagnostics.Process.Start("EXCEL.EXE", localFile);

            return filename;
        }
        private void ServerShareFile(string ordernumer, string prefix, DataRow row)
        {
            string destfile, tempfile = "";
            string fileabsname = ordernumer;

            filename = destfolder + "\\" + prefix+"_" + fileabsname + ".xls"; ;
            destfile = filename;
            if (File.Exists(fileabsname + ".xls"))
            {
                File.Delete(fileabsname + ".xls");
            }
            tempfile = CreateFile(ordernumer, row);
            if (File.Exists(destfile))
            {
                File.Delete(destfile);
            }
            File.Move(tempfile, destfile);

            if (operation == "OpenExcel")
            {
                System.Diagnostics.Process.Start("EXCEL.EXE", filename);
            }

            if (File.Exists(tempfile))
            {
                File.Delete(tempfile);
            }

        }
    }
}